package com.metaverse.backend.converter;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Converter
public class LongArrayConverter implements AttributeConverter<List<Long>, String> {
    @Override
    public String convertToDatabaseColumn(List<Long> longs) {
        if (longs != null && !longs.isEmpty())
            return StringUtils.join(longs, ",");
        return null;
    }

    @Override
    public List<Long> convertToEntityAttribute(String s) {
        if (StringUtils.isNotEmpty(s)) {
            return Arrays.stream(s.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
